package models

import (
	"github.com/astaxie/beego/orm"
	"lphoto/validations/adminValid"
	"time"
)

type MediaInfo struct {
	Id int
	Title string
	Url string
	UserInfoId int
	Status int
	Type int
	IsShow int
	CreateTime time.Time
	ShootTime string
	Longitude float32
	Latitude float32
	Address string
}

func MediaInfoAdd(v adminValid.UploadValidation, uid int) (int, error) {
	o := orm.NewOrm()
	var m MediaInfo
	m.UserInfoId = uid
	m.Title = v.Title
	m.Url = v.Url
	m.Status = 0
	m.Type = v.Type
	m.IsShow = v.IsShow
	m.CreateTime = time.Now()
	m.ShootTime = v.ShootTime
	m.Longitude = v.Longitude
	m.Latitude = v.Latitude
	m.Address = v.Address
	id, err := o.Insert(&m)
	return int(id), err
}

func MediaInfoGetCount(userId int) int{
	o := orm.NewOrm()
	cnt,_ := o.QueryTable("media_info").Filter("status__iexact",-1).Filter("user_info_id",userId).Count()
	return int(cnt)
}
func MediaInfoGetAllForAdmin() interface{}{
	o := orm.NewOrm()
	var m []*MediaInfo
	o.QueryTable("media_info").Filter("status__exact",0).All(&m)
	return m
}
func MediaInfoGetAll() interface{}{
	o := orm.NewOrm()
	var m []*MediaInfo
	o.QueryTable("media_info").Filter("is_show__exact",0).Filter("status__exact",0).All(&m)
	return m
}
func MediaInfoDelProduct(id int) (num int, err error){
	o := orm.NewOrm()
	n, e := o.QueryTable("media_info").Filter("id",id).Update(orm.Params{"status":1})
	return int(n), e
}
func MediaInfoGetForId(id int)  interface{}{
	m := new(MediaInfo)
	o := orm.NewOrm()
	o.QueryTable(m).Filter("id",id).One(m)
	return m
}